package com.dingwen.treasure.base.constant;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 　CollectionConstant: 不可变集合常量定义,注意，引用类型被 final 修饰时只是引用地址不能改变，本身内容也是可以改变的
 * 　@author dingwen
 * 　@date 2022/6/27
 */
@SuppressWarnings("ALL")
public class CollectionConstant {

    public static final List<Integer> ints1 = new ArrayList<>();

    /**
     * Arrays.asList 能保证不可变，即使没有加 final
     */
    public static final List<Integer> ints2 = Arrays.asList(1, 2);


    public static List<Integer> ints4 = Collections.unmodifiableList(ints1);

    public static void main(String[] args) {
        //System.out.println("this.ints1 = " + ints1);
        //ints1.add(11);
        //System.out.println("this.ints1 = " + ints1);

        //System.out.println("this.ints2 = " + ints2);
        //ints2.add(11);
        //System.out.println("this.ints2 = " + ints2);

        System.out.println("this.ints4 = " + ints4);
        //ints4.add(11);
        //System.out.println("this.ints4 = " + ints4);

        //Collections.unmodifiableCollection()
        //Collections.unmodifiableMap()
        //Collections.unmodifiableSet()
        //Collections.unmodifiableSortedMap()
        // ....

        for (Integer i : ints2) {
            if (i == 1) {
                System.out.println(i);
                return;
            }
            System.out.println(i);
        }
        //TODO 注意 labmda 中的return 并不能结束循环
        ints2.stream().forEach(i -> {
            if (i == 1) {
                System.out.println(i);
                return;
            }
            System.out.println(i);
        });

    }


}